package com.snow.useviewdemo.function

import android.os.Bundle
import android.text.TextUtils
import com.snow.common.tool.utils.ToastUtils
import com.snow.useviewdemo.base.BaseKtActivity
import com.snow.useviewdemo.databinding.ActivityDownloadTestBinding
import com.snow.useviewdemo.util.LogUtils
import com.snow.useviewdemo.util.download.DownLoadUtils
import com.snow.useviewdemo.util.download.OnDownloadListener
import java.io.File

/**
 * Created by zhyt
 * Date: 2022/10/8
 * Describe:
 */
class DownLoadTestActivity : BaseKtActivity<ActivityDownloadTestBinding>() {
    override fun getViewBinding() = ActivityDownloadTestBinding.inflate(layoutInflater)

    override fun setViewData(savedInstanceState: Bundle?) {
        binding.button01.setOnClickListener {
            downLoadVideo()
        }
        binding.button02.setOnClickListener {

            downLoadImg()
        }
    }

    fun downLoadImg() {
        val url = "https://scpic.chinaz.net/files/default/imgs/2022-10-05/4a3e40f0099e7da2.jpg"
        val dirName: String = filesDir.absolutePath + File.separator + "downFile"
        val fileName: String =
            "" + System.currentTimeMillis() + getSuffixName(url, ".jpg")

        DownLoadUtils.getInstance().download(
            url,
            dirName,
            fileName,
            object : OnDownloadListener {
                override fun onDownloadSuccess(file: File?) {
                    toastMessage(file?.absolutePath)
                    LogUtils.d(file?.absolutePath)
                }

                override fun onDownloading(progress: Int) {
                    LogUtils.d("downProgress:$progress")
                }

                override fun onDownloadFailed(e: Exception?) {
                    super.onDownloadFailed(e)
                    toastMessage(e?.message)
                }
            }
        )
    }

    private fun downLoadVideo() {
        val url =
            "https://cdn.changguwen.com/cms/media/20181024/0dbfb433-d976-4c54-9026-272ca2967f46-1540367091203-sd.mp4"
//        val dirName: String = filesDir.absolutePath + File.separator + "downFile"
        val dirName: String = cacheDir.absolutePath + File.separator + "downFile"
        val fileName: String =
            "" + System.currentTimeMillis() + getSuffixName(url, ".mp4")
        DownLoadUtils.getInstance().download(
            url,
            dirName,
            fileName,
            object : OnDownloadListener {
                override fun onDownloadSuccess(file: File?) {
                    toastMessage(file?.absolutePath)
                    // filesDir.absolutePath >>>>> /data/user/0/com.snow.useviewdemo/files/downFile/1665223409056.jpg
                    // cacheDir.absolutePath >>>>> /data/user/0/com.snow.useviewdemo/cache/downFile/1665223911614.mp4
                    LogUtils.d(file?.absolutePath)
                }

                override fun onDownloading(progress: Int) {
                    LogUtils.d("downProgress:$progress")
                }

                override fun onDownloadFailed(e: Exception?) {
                    super.onDownloadFailed(e)
                    toastMessage(e?.message)
                }
            }
        )
    }

    fun toastMessage(msg: String?) {
        runOnUiThread {
            ToastUtils.getInstance().showToast(msg)
        }
    }

    fun getSuffixName(url: String, defaultSuffix: String?): String? {
        if (TextUtils.isEmpty(url)) {
            return defaultSuffix
        }
        val lastIndexOf = url.lastIndexOf(".")
        val endPosition = url.indexOf("?")
        return if (lastIndexOf != -1) {
            if (endPosition != -1) {
                url.substring(lastIndexOf, endPosition)
            } else {
                url.substring(lastIndexOf)
            }
        } else {
            defaultSuffix
        }
    }
}